package com.example.hcbmsm.service.impl;

import com.example.hcbmsm.domain.Dept;
import com.example.hcbmsm.domain.DeptExample;
import com.example.hcbmsm.domain.Doc;
import com.example.hcbmsm.domain.DocExample;
import com.example.hcbmsm.mapper.DeptMapper;
import com.example.hcbmsm.mapper.DocMapper;
import com.example.hcbmsm.service.DeptService;
import com.example.hcbmsm.service.DocService;
import com.example.hcbmsm.util.HCBMSMResult;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.*;

/**
 * @author Xinghm
 * @version 1.0
 * @since 2024/10/29 22:47
 */
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private DocMapper docMapper;
    @Override
    public Map findAllDept() {
        DeptExample deptExample = new DeptExample();
        return HCBMSMResult.SUCCESS(deptMapper.selectByExample(deptExample));
    }

    @Override
    public Map list(String deptName, int page, int limit) {
        Integer total = deptMapper.selectDeptList(deptName).size();
        PageHelper.startPage(page, limit);
        List<Dept> list = deptMapper.selectDeptList(deptName);
        Map map = new HashMap<>();
        map.put("total", total);
        map.put("data", list);
        return HCBMSMResult.SUCCESS(map);
    }

    @Override
    public Map add(Dept dept) {
        DeptExample deptExample = new DeptExample();
        deptExample.createCriteria().andDeptNameEqualTo(dept.getDeptName());
        List<Dept> depts = deptMapper.selectByExample(deptExample);
        if (!depts.isEmpty() && !Objects.equals(depts.get(0).getDeptId(), dept.getDeptId())) {
            return HCBMSMResult.WARNING(400, "用户名已存在");
        }
        if (deptMapper.insertSelective(dept) == 1) {
            return HCBMSMResult.SUCCESS();
        }
        return HCBMSMResult.ERROR();
    }

    @Override
    public Map renew(Dept dept) {
        DeptExample deptExample = new DeptExample();
        deptExample.createCriteria().andDeptNameEqualTo(dept.getDeptName());
        List<Dept> depts = deptMapper.selectByExample(deptExample);
        if (!depts.isEmpty() && !Objects.equals(depts.get(0).getDeptId(), dept.getDeptId())) {
            return HCBMSMResult.WARNING(400, "用户名已存在");
        }
        dept.setModifyTime(new Date());
        if (deptMapper.updateByPrimaryKeySelective(dept) == 1) {
            return HCBMSMResult.SUCCESS();
        }
        return HCBMSMResult.ERROR();
    }

    @Override
    public Map remove(Long deptId) {
        DocExample docExample = new DocExample();
        docExample.createCriteria().andDeptIdEqualTo(deptId);
        if (!docMapper.selectByExample(docExample).isEmpty()) {
            return HCBMSMResult.ERROR("该部门有医生未删除！");
        }
        if (deptMapper.deleteByPrimaryKey(deptId) == 1) {
            return HCBMSMResult.SUCCESS();
        }
        return HCBMSMResult.ERROR();
    }
}
